<?php

/**
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.implode.php
 * Type:     function
 * Name:     implode
 * Version:  1.0
 * Date:     Jan 09, 2003
 * Purpose:  implode an array
 * Input:
 *         - from = array to implode (required)
 *         - delim = delimiter to implode with (optional, default none)
 *         - none = output when the array is empty (optional, default none)
 *         - pre = string to prepend to each array entry (optional, default none)
 *         - post = string to append to each array entry (optional, default none)
 *
 * Example:  $foo = array('a', 'b', 'c');
 *           {implode from=$foo delim="," pre="[" post="]" none="Empty list!"}
 *
 * Output:   [a],[b],[c]
 *
 * Install:  Just drop into the plugin directory.
 *
 * Author:   Cal Henderson <cal@iamcal.com>
 * -------------------------------------------------------------
 */
function smarty_function_implode($params, &$smarty)
{
    if (!isset($params['from']))
    {
        $smarty->trigger_error("implode: missing 'from' parameter", E_USER_WARNING);
        return;
    }

    if (!count($params['from']))
    {
        return isset($params['none']) ? $params['none'] : '';
    }

    if (!isset($params['pre']))
    {
        $params['pre'] = '';
    }

    if (!isset($params['post']))
    {
        $params['post'] = '';
    }

    if (!isset($params['delim']))
    {
        $params['delim'] = '';
    }

    foreach ($params['from'] as $item)
    {
        $src[] = $params['pre'] . $item . $params['post'];
    }

    return implode($params['delim'], $src);
}
